﻿using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MuaDeal
{
    public partial class XemDSSanPham : Form
    {
        public XemDSSanPham()
        {
            InitializeComponent();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void XemDSSanPham_Load(object sender, EventArgs e)
        {
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = "sp_LayDanhSachSanPham";
                string proc1 = "sp_LayTinhTrangSanPham";
                string proc2 = "sp_LayDSLinhVuc";
                string proc3 = "sp_LayDanhSachCongTy";
                DataTable dt = new DataTable();
                DataTable dttinhtrang = new DataTable();
                DataTable dtlinhvuc = new DataTable();
                DataTable dtcongty=new DataTable();
                dt = helper.ExecuteQueryStore(proc);
                dttinhtrang = helper.ExecuteQueryStore(proc1);
                dtlinhvuc = helper.ExecuteQueryStore(proc2);
                dtcongty=helper.ExecuteQueryStore(proc3);
                helper.Close();
                foreach (DataRow row in dt.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = row[0].ToString();
                    item.SubItems.Add(row[2].ToString());
                    //linh vuc
                    //item.SubItems.Add(row[1].ToString());
                    foreach (DataRow lvuc in dtlinhvuc.Rows)
                    {
                        if (lvuc[0].ToString() == row[1].ToString())
                        {
                            item.SubItems.Add(lvuc[1].ToString());
                        }
                    }
                    item.SubItems.Add(row[3].ToString());
                    item.SubItems.Add(row[4].ToString());
                    item.SubItems.Add(row[5].ToString());
                    item.SubItems.Add(row[6].ToString());
                    item.SubItems.Add(row[7].ToString());
                    foreach (DataRow ttrang in dttinhtrang.Rows)
                    {
                        if (ttrang[0].ToString() == row[8].ToString())
                        {
                            item.SubItems.Add(ttrang[1].ToString());
                        }
                    }
                    item.SubItems.Add(row[7].ToString());
                    //congty
                    //item.SubItems.Add(row[10].ToString());
                    foreach (DataRow cty in dtcongty.Rows)
                    {
                        if (cty[0].ToString() == row[10].ToString())
                        {
                            item.SubItems.Add(cty[1].ToString());
                        }
                    }
                    item.SubItems.Add(row[9].ToString());
                    listView1.Items.Add(item);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }

        private void rdbtndung_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnxemlai_Click(object sender, EventArgs e)
        {
            if (rdbtnreadcommitted.Checked==true)
            {
                try
                {
                    
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    string proc = "sp_LayDanhSachSanPhamDeLay";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    string proc3 = "sp_LayDanhSachCongTy";
                    DataTable dt = new DataTable();
                    DataTable dttinhtrang = new DataTable();
                    DataTable dtlinhvuc = new DataTable();
                    DataTable dtcongty = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    dtcongty = helper.ExecuteQueryStore(proc3);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        //linh vuc
                        //item.SubItems.Add(row[1].ToString());
                        foreach (DataRow lvuc in dtlinhvuc.Rows)
                        {
                            if (lvuc[0].ToString() == row[1].ToString())
                            {
                                item.SubItems.Add(lvuc[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[7].ToString());
                   
                        foreach (DataRow cty in dtcongty.Rows)
                        {
                            if (cty[0].ToString() == row[10].ToString())
                            {
                                item.SubItems.Add(cty[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }
            }
            else if (rdbtnsai.Checked==true)
            {
                try
                {
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    string proc = "sp_LayDanhSachSanPhamUncommitted";
                    string proc1 = "sp_LayTinhTrangSanPham";
                    string proc2 = "sp_LayDSLinhVuc";
                    string proc3 = "sp_LayDanhSachCongTy";
                    DataTable dt = new DataTable();
                    DataTable dttinhtrang = new DataTable();
                    DataTable dtlinhvuc = new DataTable();
                    DataTable dtcongty = new DataTable();
                    dt = helper.ExecuteQueryStore(proc);
                    dttinhtrang = helper.ExecuteQueryStore(proc1);
                    dtlinhvuc = helper.ExecuteQueryStore(proc2);
                    dtcongty = helper.ExecuteQueryStore(proc3);
                    helper.Close();
                    listView1.Items.Clear();
                    foreach (DataRow row in dt.Rows)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = row[0].ToString();
                        item.SubItems.Add(row[2].ToString());
                        //linh vuc
                        //item.SubItems.Add(row[1].ToString());
                        foreach (DataRow lvuc in dtlinhvuc.Rows)
                        {
                            if (lvuc[0].ToString() == row[1].ToString())
                            {
                                item.SubItems.Add(lvuc[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[3].ToString());
                        item.SubItems.Add(row[4].ToString());
                        item.SubItems.Add(row[5].ToString());
                        item.SubItems.Add(row[6].ToString());

                        foreach (DataRow ttrang in dttinhtrang.Rows)
                        {
                            if (ttrang[0].ToString() == row[8].ToString())
                            {
                                item.SubItems.Add(ttrang[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[7].ToString());

                        foreach (DataRow cty in dtcongty.Rows)
                        {
                            if (cty[0].ToString() == row[10].ToString())
                            {
                                item.SubItems.Add(cty[1].ToString());
                            }
                        }
                        item.SubItems.Add(row[9].ToString());
                        listView1.Items.Add(item);
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }
            }
            
        }

        private void rdbtnsai_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}